package com.zzptc.ly.bookcity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zzptc.ly.bookcity.Utils.ProcessText;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ReadActivity extends AppCompatActivity {
    ScrollView scroll;
    Button getPre,getNext;
    TextView textView,pagetxt;
    String filepath="/sdcard/Download/";
    ProcessText processText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
        initView();
        setListener();
    }
    private void getPage(){
        pagetxt.setText("第"+processText.currentpage+"页/共"+processText.pages+"页");
    }
    private void setListener() {
        View.OnClickListener listener=new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String content="";
                switch (v.getId()){
                    case R.id.getPre:
                        content=processText.getPre();
                        textView.setText(content);
                        getPage();
                        scroll.scrollTo(0,0);
                        break;
                    case R.id.getNext:
                        content=processText.getPre();
                        textView.setText(content);
                        getPage();
                        scroll.scrollTo(0,0);
                        break;
                }
            }
        };
        getNext.setOnClickListener(listener);
        getPre.setOnClickListener(listener);
    }

    private void initView() {
        scroll=findViewById(R.id.scroll);
        textView=findViewById(R.id.readtxt);
        getNext=findViewById(R.id.getNext);
        getPre=findViewById(R.id.getPre);
        pagetxt=findViewById(R.id.pagetxt);
        Intent it=getIntent();
        String bookname=it.getStringExtra("bookname");
        String content=readAssetsText(bookname);
        File file=writeTxtToFile(content,filepath,bookname);

        processText=new ProcessText(file,0);
        content=processText.getNext();
        textView.setText(content);
        getPage();
    }

    private String readAssetsText(String bookname) {
        try {
            InputStream is =getAssets().open(bookname+".txt");
            int size=is.available();
            byte[] buffer=new byte[size];
            is.read(buffer);
            is.close();
            String text=new String(buffer,"utf-8");
            return text;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public File writeTxtToFile(String strcontent,String filepath,String fileName){
        String strFilePath=filepath+fileName;
        String strContent=strcontent+"\r\n";
        //内容结尾换行
        try {
            File file=new File(strFilePath);
            FileOutputStream fos=new FileOutputStream(file,false);
            //如果文件不存在，输出流则会创建一个文件，false为不追加，也就是覆盖了
            fos.write(strContent.getBytes());
            //内容转为字节并写入file对象
            fos.close();
            return  file;
        }catch (Exception e){
            Log.e("TestFile", "Error on File: "+e );
        }
        return null;
    }
}
